package com.crr.mall.product.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtils {
    @Autowired
    RedisTemplate<String,Object> redisTemplate;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    public void set(String key, Object value) {
        stringRedisTemplate.opsForValue().set(key, value.toString());
    }

    /**
     * 设置key-value 有效期
     *
     * @param key
     * @param value
     * @param timeout 有效期 单位是s
     */
    public void setEx(String key, Object value, long timeout) {
        redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
    }

    /**
     * key不存在时保存成功
     *
     * @param key
     * @param value
     * @return
     */
    public boolean setNx(String key, String value) {
        return redisTemplate.opsForValue().setIfAbsent(key, value);
    }

    /**
     * key不存在时保存成功
     *
     * @param key
     * @param value
     * @return
     */
    public boolean setNx(String key, String value, long timeout) {
        return redisTemplate.opsForValue().setIfAbsent(key, value, timeout, TimeUnit.SECONDS);
    }

    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    public void lpush(String key, String value) {
        redisTemplate.opsForList().leftPush(key, value);
    }

    public void lrange(String key, int start, int end) {
        redisTemplate.opsForList().range(key, start, end);
    }

    /**
     * 设置过期时间 单位是s
     *
     * @param key
     * @param timeout
     */
    public void expire(String key, long timeout) {
        redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
    }

    public Boolean del(String key) {
        return redisTemplate.delete(key);
    }

    public Object hget(String key, String filed) {
        HashOperations operations = stringRedisTemplate.opsForHash();
        return operations.get(key, filed);
    }

    public void hset(String key, String field, Object value) {
        stringRedisTemplate.opsForHash().put(key, field, value.toString());
    }

    /**
     * 返回指定 模式所有的key
     *
     * @param pattern
     * @return
     */
    public Set<String> keys(String pattern) {
        return redisTemplate.keys(pattern);
    }

    public Long hdecr(String key, String field) {
        return stringRedisTemplate.opsForHash().increment(key, field, -1l);
    }

    public Long decr(String key){
        return stringRedisTemplate.opsForValue().increment(key,-1);
    }

    public Long eval(String script, List<String> keys) {
        RedisScript<Long> redisScript = new DefaultRedisScript(script, Long.class);
        return stringRedisTemplate.execute(redisScript, keys);
    }
}
